package com.taotao.manage.controller;

import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.manage.pojo.ItemCat;
import com.taotao.manage.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/item/cat")
public class ItemCatController {

    @Autowired
    private ItemCatService itemCatService;

    /**
     * 查询商品种类列表
     */
    @RequestMapping
    public List<EasyUITreeNode> getItemCatList(@RequestParam(value="id",defaultValue = "0")long parentId ){
        ItemCat itemCatWhere = new ItemCat();
        itemCatWhere.setParentId(parentId);
        List<ItemCat> list = itemCatService.queryListByWhere(itemCatWhere);

        ArrayList<EasyUITreeNode> resultList = new ArrayList<>();
        for (ItemCat itemCat : list) {
            EasyUITreeNode node=new EasyUITreeNode();
            node.setId(itemCat.getId());
            node.setText(itemCat.getName());
            node.setState(itemCat.getIsParent()?"closed":"open");
            resultList.add(node);
        }
        return resultList;
    }
    /**
     * 根据类目id查询商品类目
     */
    @RequestMapping(value = "/{itemCatId}",method = RequestMethod.GET)
    public ItemCat queryItemCatByItemCatId(@PathVariable("itemCatId")Long itemCatId){
        return itemCatService.queryById(itemCatId);
    }

}
